/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package proyecto.so1;

import java.net.*;
import java.io.*;

/**
 *
 * @author antonio
 */
public class GetCommit extends Thread implements Runnable{
    
    private boolean active;
    
    
    public GetCommit(){
        active = true;
    }
    
    @Override
    public void run(){
        while(active){
            try{ 
                int state = sender();
                if(state >-1){
                    principal.table = principal.states.get(state);
                    principal.table.set(1, false);
                    principal.states.set(state, principal.table);
                    this.sleep(500);
                }
            }
            catch(Exception e){ }
        }
    }
    
    
    private int sender(){
        int sender = -1;    
            try{
                Socket client = new Socket("192.168.1.8", 10300);
                client.setReuseAddress(true);
                System.out.println("Connected to " + client.getInetAddress() + " on port "
                + client.getPort() + " from port " + client.getLocalPort() + " of "
                + client.getLocalAddress());
                InputStream input = client.getInputStream();
                DataInputStream data = new DataInputStream(input);
                sender = data.readInt();
                client.close();
            }
            catch(Exception e){
                try{
                    Socket client = new Socket("192.168.1.20", 10300);
                    client.setReuseAddress(true);
                    System.out.println("Connected to " + client.getInetAddress() + " on port "
                    + client.getPort() + " from port " + client.getLocalPort() + " of "
                    + client.getLocalAddress());
                    InputStream input = client.getInputStream();
                    DataInputStream data = new DataInputStream(input);
                    sender = data.readInt();
                    client.close();
                }
                catch(Exception ex){ }
            }
            return sender;
    }
    
}
